package com.hrt.manage.product.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hrt.bean.ProductDetails;
import com.hrt.bean.ProductInfor;
import com.hrt.manage.product.dao.ProInforDao;
import com.hrt.manage.product.dao.ProductDetailsDao;
import com.hrt.manage.product.service.ProDetailsService;
import com.hrt.manage.product.service.ProInforService;

@Service
public class ProductDetailsServiceImpl implements ProDetailsService {

	
	@Autowired
	private ProductDetailsDao dao;
	
	@Autowired
	private ProInforService proInforSvr;
	
	@Autowired
	private ProInforDao proInforDao;
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void saveDetail(ProductDetails detail) {
		
		
		try {
			dao.saveEntity(detail);
		} catch (Exception e) {
			e.printStackTrace();
		}
		proInforSvr.updateTotal(detail.getProductInfor());
		
	}

	@Override
	public List<ProductDetails> queryByPage(int pageIndex, int pageSize) {
		
		return dao.queryByPage(pageIndex, pageSize);
	}

	@Override
	@Transactional
	public void deleteById(Long[] proIds) {
		
		ProductInfor product=null;
		List<ProductDetails> list = dao.getByIds(proIds);
		for (ProductDetails detail : list) {
			detail.setIsdelete(new Integer(1));
			dao.updateEntity(detail);
			if(product==null){
			 product=detail.getProductInfor();
			}
			
		}
		proInforSvr.updateTotal(product);
	}

	@Override
	public ProductDetails getDetailById(Long deId) {
		
		return dao.getById(deId);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void udateDetail(ProductDetails details) {
		
		dao.updateEntity(details);
		proInforSvr.updateTotal(details.getProductInfor());
		
	}

	@Override
	public Long totalCount() {
		
		return dao.totalCount();
	}

	@Override
	public List<ProductDetails> queryByProId(Long proId, Integer pageIndex,
			Integer pageSize) {
		List<Map<String, Object>> conditions=new ArrayList<Map<String, Object>>();
		List<ProductDetails> list=new ArrayList<ProductDetails>();
		Map<String, Object> con=new HashMap<String, Object>();
		con.put("field", "proId");
		con.put("value", proId);
		
		Map<String, Object> delcon=new HashMap<String, Object>();
		delcon.put("field", "isdelete");
		delcon.put("value", 0);
		conditions.add(con);
		list=dao.queryPageByCondition(conditions,pageIndex, pageSize);
		
		return list;
	}

	@Override
	public List<ProductDetails> queryByProId(Long proId) {
		List<Map<String, Object>> conditions=new ArrayList<Map<String, Object>>();
		List<ProductDetails> list=new ArrayList<ProductDetails>();
		Map<String, Object> con=new HashMap<String, Object>();
		con.put("field", "proId");
		con.put("value", proId);
		
		Map<String, Object> delcon=new HashMap<String, Object>();
		delcon.put("field", "isdelete");
		delcon.put("value", new Integer(0));
		conditions.add(con);
		conditions.add(delcon);
		list=dao.queryByCondition(conditions);
		
		return list;
	}

	@Override
	public List<ProductDetails> queryProducts(String condtionStr) {
		
		List<ProductDetails> list = new ArrayList<ProductDetails>();

		list = dao.queryProducts(condtionStr);
		return list;
	}

	/**
	 * 查询所有
	 * */
	@Override
	public List<ProductDetails> searchDelAll() {
		
		return this.dao.findDelAll();
	}

	@Override
	public ProductDetails getDetailByProIdAndRuleId(Long proId, Long ruleId) {
		return dao.getDetailByProIdAndRuleId(proId,ruleId);
	}

	/**通过产品id查找产品规格*/
	@Override
	public List<ProductDetails> getProDetailByProId(Long proId) {
		return this.dao.getProDetailByProId(proId);
	}
	
	/**
	 * 按id列表查询实体
	 * */
	@Override
	public ProductDetails getById(Long id) {
		return this.dao.getById(id);
	}

	@Override
	public Long getProInforDetailTotals(Long ruleId) {
		// TODO Auto-generated method stub
		Long total=dao.getProInforDetailTotals(ruleId);
		if(total==null)
		{
			total=0l;
		}
		return total;
	}

}
